home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / A-Z / Q / QuickDraw3D / Simple QuickDraw 3D View / UQD3DViewer.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  14.5 KB  |  501 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UQD3DViewerView.cp
  3. // Copyright © 1996 by Geoffrey J. Clapp. All rights reserved. 
  4. //----------------------------------------------------------------------------------------
  5.  
  6.  
  7. #ifndef __UQD3DViewer__
  8. #include "UQD3DViewer.h"
  9. #endif
  10.  
  11. //MacApp
  12. #if qDrag
  13.  
  14. #ifndef __UDRAGDROP__
  15. #include "UDragDrop.h"
  16. #endif
  17.  
  18. #endif
  19.  
  20. //QuickDraw3D
  21. #ifndef   QD3DViewer_h
  22. #include "QD3DViewer.h"
  23. #endif
  24.  
  25. #ifndef   QD3DView_h
  26. #include "QD3DView.h" //for rendering services
  27. #endif
  28.     
  29. //========================================================================================
  30. // Global Initialization Procedure
  31. //========================================================================================
  32. #undef Inherited
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // InitUQD3DViewerView: 
  36. //----------------------------------------------------------------------------------------
  37. #pragma segment DlgInit
  38.  
  39. void InitUQD3DViewerView()
  40. {
  41.     // So the linker doesn't dead strip class info 
  42.     MA_REGISTER_CLASS(TQD3DViewerView);
  43.     
  44. #if qDebug
  45. #if qDrag
  46.     if(!HasDragManager())
  47.         DebugStr("\p Due to a bug in QD3D (1.0.3), the viewer will crash without the DragManager installed.");
  48. #endif
  49. #endif
  50. } // InitUQD3DViewerView 
  51.  
  52. //========================================================================================
  53. // CLASS TQD3DViewerView
  54. //========================================================================================
  55. #undef Inherited
  56. #define Inherited TView
  57.  
  58. #pragma segment AOpen
  59. MA_DEFINE_CLASS_M1(TQD3DViewerView, Inherited);
  60.  
  61. //----------------------------------------------------------------------------------------
  62. // TQD3DViewerView constructor 
  63. //----------------------------------------------------------------------------------------
  64. #pragma segment AOpen
  65.  
  66. TQD3DViewerView::TQD3DViewerView() :
  67.                 fViewer(NULL),
  68.                 fUseManualCreation(FALSE)
  69. {
  70. } // TQD3DViewerView::TQD3DViewerView 
  71.  
  72. //----------------------------------------------------------------------------------------
  73. // TQD3DViewerView destructor
  74. //----------------------------------------------------------------------------------------
  75. #pragma segment MADestructorRes
  76.  
  77. TQD3DViewerView::~TQD3DViewerView()
  78. {
  79.     this->RemoveViewerFromView();
  80. }// TQD3DViewerView destructor
  81.     
  82. //----------------------------------------------------------------------------------------
  83. // TQD3DViewerView::IQD3DViewerView: 
  84. //----------------------------------------------------------------------------------------
  85. #pragma segment AOpen
  86.  
  87. void TQD3DViewerView::IQD3DViewerView(TDocument* itsDocument,
  88.                                         TView* itsSuperView,
  89.                                          const VPoint& itsLocation,
  90.                                          const VPoint& itsSize)
  91. {
  92.     this->IView(itsDocument,itsSuperView,itsLocation,itsSize, sizeFixed, sizeFixed);
  93. } // TQD3DViewerView::IQD3DViewerView 
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // TQD3DViewerView::Draw: 
  97. //----------------------------------------------------------------------------------------
  98. #pragma segment ARes
  99.  
  100. void TQD3DViewerView::Draw(const VRect& area)
  101. {
  102.  
  103.     Inherited::Draw(area);
  104.  
  105.     if(fViewer)
  106.     {
  107.         this->GetWindow()->Focus();
  108.         Q3ViewerDraw(fViewer);
  109.         this->Focus();
  110.     }
  111.  
  112. } // TQD3DViewerView::Draw 
  113.  
  114. //----------------------------------------------------------------------------------------
  115. // TQD3DViewerView::DoPostCreate: 
  116. //----------------------------------------------------------------------------------------
  117. #pragma segment AOpen
  118.     
  119. void TQD3DViewerView::DoPostCreate(TDocument* itsDocument) 
  120. {
  121.     Inherited::DoPostCreate(itsDocument);
  122.     
  123.     if(!fUseManualCreation)
  124.         this->AddViewerToView(kQ3ViewerDefault);
  125.         
  126. } // TQD3DViewerView::DoPostCreate 
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // TQD3DViewerView::DoSetupMenus: 
  130. //----------------------------------------------------------------------------------------
  131. #pragma segment ARes
  132.             
  133. void TQD3DViewerView::DoSetupMenus()
  134. {
  135.     Inherited::DoSetupMenus();
  136.     
  137.     //The base class will handle cut, copy, paste and clear.
  138.  
  139.     if(fViewer)
  140.     {
  141.         Boolean hasImage = FALSE;
  142.         
  143.         if(Q3ViewerGetState(fViewer) == kQ3ViewerHasModel)
  144.             hasImage = TRUE;
  145.             
  146.         Enable(cCut, hasImage);
  147.         Enable(cCopy, hasImage);
  148.         gClipboardMgr->CanPaste('3DMF'); //can paste a 3DMF file
  149.         Enable(cClear, hasImage);
  150.     }
  151.  
  152. } // TQD3DViewerView::DoSetupMenus 
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // TQD3DViewerView::DoMenuCommand:
  156. //----------------------------------------------------------------------------------------
  157. #pragma segment ASelCommand
  158.  
  159. void TQD3DViewerView::DoMenuCommand(CommandNumber aCommandNumber)
  160. {
  161.     switch (aCommandNumber) 
  162.     {
  163.         //will do these as undo-able commands, but for now will just do the quick away.
  164.         
  165.         case cCut:
  166.             Q3ViewerCut(fViewer);
  167.         break;
  168.         
  169.         case cCopy:
  170.             Q3ViewerCopy(fViewer);
  171.         break;
  172.         
  173.         case cPaste:
  174.             Q3ViewerPaste(fViewer);
  175.         break;
  176.         
  177.         case cClear:
  178.             Q3ViewerClear(fViewer);
  179.         break;
  180.         
  181.         default:
  182.             Inherited::DoMenuCommand(aCommandNumber);
  183.         break;
  184.     }
  185. } // TQD3DViewerView::DoMenuCommand 
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // TQD3DViewerView::DoMouseCommand: 
  189. //----------------------------------------------------------------------------------------
  190. #pragma segment ASelCommand
  191.  
  192. void TQD3DViewerView::DoMouseCommand(VPoint& theMouse,
  193.                                   TToolboxEvent*  event,
  194.                                   CPoint  hysteresis)
  195. {
  196.     Boolean handledEvent = FALSE;
  197.  
  198.     if (fViewer)
  199.     {
  200.         this->GetWindow()->Focus();
  201.         handledEvent = Q3ViewerEvent(fViewer, &(event->fEventRecord));
  202.         this->Focus();
  203.     }
  204.  
  205.     //Pass the event off to MacApp if QD3D did not handle it
  206.     if (!handledEvent)
  207.         Inherited::DoMouseCommand(theMouse,event,hysteresis);
  208.     
  209. } // TQD3DViewerView::DoMouseCommand 
  210.  
  211. //----------------------------------------------------------------------------------------
  212. // TQD3DViewerView::AddViewerToView: 
  213. //----------------------------------------------------------------------------------------
  214. #pragma segment ARes
  215.  
  216. void TQD3DViewerView::AddViewerToView(unsigned long theViewerFlags)
  217. {
  218.     VRect theFrame; 
  219.     this->GetFrame(theFrame);
  220.     //inset to keep frame adorners, etc., if any
  221.     VPoint thePoint(1,1);
  222.     theFrame.Inset(thePoint);
  223.     CRect theCRectFrame = theFrame.ToRect();
  224.     
  225.     GrafPtr theWindowPort = this->GetWindow()->GetGrafPort();
  226.  
  227.     fViewer = Q3ViewerNew((CGrafPtr)theWindowPort,theCRectFrame,theViewerFlags);
  228.     
  229. } // TQD3DViewerView::AddViewerToView 
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // TQD3DViewerView::RemoveViewerFromView: 
  233. //----------------------------------------------------------------------------------------
  234. #pragma segment ARes
  235.  
  236. void TQD3DViewerView::RemoveViewerFromView()
  237. {
  238.     if(fViewer)
  239.         FailOSErr(Q3ViewerDispose(fViewer));
  240. } // TQD3DViewerView::RemoveViewerFromView 
  241.  
  242. //----------------------------------------------------------------------------------------
  243. // TQD3DViewerView::SetRenderer: 
  244. //----------------------------------------------------------------------------------------
  245. #pragma segment ARes
  246.  
  247. void TQD3DViewerView::SetRenderer(TQ3ObjectType renderType, Boolean redraw)
  248. {
  249.  
  250.     TQ3ViewObject     theView = Q3ViewerGetView(fViewer);
  251.     TQ3Status         the3DStatus = Q3View_SetRendererByType(theView, renderType);
  252.  
  253.     if (the3DStatus != kQ3Success )
  254.     {
  255.         Q3Object_Dispose(theView) ;  //this should undo anything we may have messed
  256.                                      //up trying to set the render
  257.         Failure(0,0);//••• need better error handling
  258.     }
  259.  
  260.     if(redraw)
  261.         this->ForceRedraw();
  262. } // TQD3DViewerView::SetRenderer 
  263.  
  264. //----------------------------------------------------------------------------------------
  265. // TQD3DViewerView::GetRenderer: 
  266. //----------------------------------------------------------------------------------------
  267. #pragma segment ARes
  268.  
  269. TQ3RendererObject* TQD3DViewerView::GetRenderer()
  270. {
  271.     TQ3RendererObject* theRenderer = NULL;
  272.  
  273.     if(fViewer)
  274.     {
  275.         TQ3ViewObject     theView = Q3ViewerGetView(fViewer);
  276.         TQ3Status         the3DStatus = Q3View_GetRenderer(theView,theRenderer);
  277.         
  278.         if (the3DStatus != kQ3Success )
  279.         {
  280.             Q3Object_Dispose(theView) ;  //this should undo anything we may have messed
  281.                                          //up trying to get the render
  282.             Failure(0,0); //•••clean this up later
  283.  
  284.         }
  285.     }
  286. #if qDebug
  287.     else
  288.     {
  289.         DebugStr("\p Attempted to GetRenderer without a viewer object.");
  290.     }
  291. #endif
  292.     return theRenderer;
  293. } // TQD3DViewerView::GetRenderer 
  294.  
  295. //----------------------------------------------------------------------------------------
  296. // TQD3DViewerView::AsPict: 
  297. //----------------------------------------------------------------------------------------
  298. #pragma segment ARes
  299.  
  300. PicHandle TQD3DViewerView::AsPict()
  301. {
  302.     PicHandle thePicHandle = NULL;
  303.     
  304.     if(fViewer)
  305.     {
  306.         thePicHandle = Q3ViewerGetPict(fViewer);
  307.         //•••need error case code. What if it failed?
  308.     }
  309. #if qDebug
  310.     else
  311.     {
  312.         DebugStr("\p Attempted to get a Pict without a viewer object.");
  313.     }
  314. #endif
  315.  
  316.     return thePicHandle;
  317. } // TQD3DViewerView::AsPict() 
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // TQD3DViewerView::SetBackgroundColor: 
  321. /*
  322.     Note: QuickDraw3D uses it's own color structures. When calling this method, if you have
  323.           an RGB value handy that you want to use (maybe you called GetColor), the best thing
  324.           to do is to get each value from your RGB.x, divide it by 65535, and stuff it back in
  325.           the TQ3ColorARGB structure.
  326.           
  327.           Or, you can just use the easy overloaded version of this function...
  328.       
  329. */
  330. //----------------------------------------------------------------------------------------
  331. #pragma segment ARes
  332.  
  333. void TQD3DViewerView::SetBackgroundColor(TQ3ColorARGB* newColor,Boolean redraw)
  334. {
  335.  
  336.     FailOSErr(Q3ViewerSetBackgroundColor(fViewer,newColor));
  337.     if(redraw)
  338.         this->Update();
  339.  
  340. } // TQD3DViewerView::SetBackgroundColor() 
  341.  
  342. //----------------------------------------------------------------------------------------
  343. // TQD3DViewerView::SetBackgroundColor: 
  344. //----------------------------------------------------------------------------------------
  345. #pragma segment ARes
  346.  
  347. void TQD3DViewerView::SetBackgroundColor(RGBColor newColor, float alpha, Boolean redraw)
  348. {
  349.     TQ3ColorARGB theQ3Color;
  350.     float maxColor = 65535.0;
  351.      
  352.     theQ3Color.r = newColor.red/maxColor;
  353.     theQ3Color.g = newColor.green/maxColor;
  354.     theQ3Color.b = newColor.blue/maxColor;
  355.     
  356.     theQ3Color.a = alpha;
  357.  
  358.     this->SetBackgroundColor(&theQ3Color,redraw);
  359.     
  360. } // TQD3DViewerView::SetBackgroundColor() 
  361.  
  362. //----------------------------------------------------------------------------------------
  363. // TQD3DViewerView::GetRGBBackgroundColor: 
  364. //----------------------------------------------------------------------------------------
  365. #pragma segment ARes
  366.  
  367. RGBColor TQD3DViewerView::GetRGBBackgroundColor()
  368. {
  369.     RGBColor      theRGBColor;
  370.     TQ3ColorARGB theQ3Color;
  371.     
  372.     FailOSErr(Q3ViewerGetBackgroundColor(fViewer,&theQ3Color));
  373.     
  374.     float maxColor = 65535.0;
  375.      
  376.     theRGBColor.red     = theQ3Color.r * maxColor;
  377.     theRGBColor.green     = theQ3Color.g * maxColor;
  378.     theRGBColor.blue     = theQ3Color.b * maxColor;
  379.     
  380.     return theRGBColor;
  381.     
  382. } // TQD3DViewerView::GetRGBBackgroundColor() 
  383.  
  384. //----------------------------------------------------------------------------------------
  385. // TQD3DViewerView::GetBackgroundColor: 
  386. //----------------------------------------------------------------------------------------
  387. #pragma segment ARes
  388.  
  389. TQ3ColorARGB* TQD3DViewerView::GetBackgroundColor()
  390. {
  391.     TQ3ColorARGB* theQ3Color = NULL;
  392.     
  393.     FailOSErr(Q3ViewerGetBackgroundColor(fViewer,theQ3Color));
  394.  
  395.     return theQ3Color;
  396.     
  397. } // TQD3DViewerView::GetBackgroundColor() 
  398.  
  399. //----------------------------------------------------------------------------------------
  400. // TQD3DViewerView::DoSetCursor: 
  401. //----------------------------------------------------------------------------------------
  402. #pragma segment ARes
  403.  
  404. void TQD3DViewerView::DoSetCursor(const VPoint& localPoint,
  405.                                     RgnHandle cursorRegion)
  406. {
  407.     //•••Needs to be fixed - does not work right, and the localpoint needs to be
  408.     //in window coor, not view coords.
  409.     CPoint localQDPoint;
  410.     localQDPoint = this->ViewToQDPt(localPoint);
  411.     
  412.     Boolean handled = Q3ViewerAdjustCursor(this->fViewer,localQDPoint);
  413.     
  414.     if(!handled)
  415.         TView::DoSetCursor(localPoint,cursorRegion);
  416.         
  417. } // TQD3DViewerView::SetCreationType
  418.  
  419.  
  420. //----------------------------------------------------------------------------------------
  421. // TQD3DViewerView::SetCreationType: 
  422. //----------------------------------------------------------------------------------------
  423. #pragma segment ARes
  424.  
  425. void TQD3DViewerView::SetCreationType(Boolean UseManual)
  426. {
  427.     fUseManualCreation = UseManual;
  428. } // TQD3DViewerView::SetCreationType
  429.  
  430. //----------------------------------------------------------------------------------------
  431. // TQD3DViewerView::SetViewerFlags: 
  432. //----------------------------------------------------------------------------------------
  433. #pragma segment ARes
  434.  
  435. void TQD3DViewerView::SetViewerFlags(unsigned long theViewerFlags)
  436. {
  437.     FailOSErr(Q3ViewerSetFlags(fViewer,theViewerFlags));
  438. } // TQD3DViewerView::SetViewerFlags
  439.  
  440. //----------------------------------------------------------------------------------------
  441. // TQD3DViewerView::GetViewerFlags: 
  442. //----------------------------------------------------------------------------------------
  443. #pragma segment ARes
  444.  
  445. unsigned long TQD3DViewerView::GetViewerFlags()
  446. {
  447.     unsigned long theFlags;
  448.     
  449.     if(fViewer)
  450.     {
  451.         theFlags = Q3ViewerGetFlags(fViewer);
  452.     }
  453.     else
  454.     {
  455. #if qDebug
  456.         DebugStr("\p Attempted to GetViewerFlags without a viewer object.");
  457. #endif
  458.         theFlags = 0;
  459.     }
  460.  
  461.      return theFlags;
  462. } // TQD3DViewerView::GetViewerFlags
  463.  
  464. //----------------------------------------------------------------------------------------
  465. // TQD3DViewerView::SetCurrentButton: 
  466. //----------------------------------------------------------------------------------------
  467. #pragma segment ARes
  468.  
  469. void TQD3DViewerView::SetCurrentButton(unsigned long theButton)
  470. {
  471.     FailOSErr(Q3ViewerSetCurrentButton(fViewer,theButton));
  472. } // TQD3DViewerView::SetCurrentButton
  473.  
  474. //----------------------------------------------------------------------------------------
  475. // TQD3DViewerView::GetCurrentButton: 
  476. //----------------------------------------------------------------------------------------
  477. #pragma segment ARes
  478.  
  479. unsigned long TQD3DViewerView::GetCurrentButton()
  480. {
  481.     unsigned long theButton = 0;
  482.     
  483.     if (fViewer)
  484.     {
  485.         theButton = Q3ViewerGetCurrentButton(fViewer);
  486.     }
  487. #if qDebug
  488.     else
  489.     {
  490.         DebugStr("\p Attempted to GetCurrentButton without a viewer object.");
  491.     }
  492. #endif
  493.     
  494.     return theButton;
  495. } // TQD3DViewerView::GetCurrentButton
  496.  
  497. //----------------------------------------------------------------------------------------
  498. // End of UQD3DViewerView.cp
  499.  
  500. #pragma segment Inline
  501.